home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Libraries / mui / browser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  10.0 KB  |  420 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1993-1997, Silicon Graphics, Inc.
  3.  * ALL RIGHTS RESERVED 
  4.  * Permission to use, copy, modify, and distribute this software for 
  5.  * any purpose and without fee is hereby granted, provided that the above
  6.  * copyright notice appear in all copies and that both the copyright notice
  7.  * and this permission notice appear in supporting documentation, and that 
  8.  * the name of Silicon Graphics, Inc. not be used in advertising
  9.  * or publicity pertaining to distribution of the software without specific,
  10.  * written prior permission. 
  11.  *
  12.  * THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS"
  13.  * AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,
  14.  * INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR
  15.  * FITNESS FOR A PARTICULAR PURPOSE.  IN NO EVENT SHALL SILICON
  16.  * GRAPHICS, INC.  BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,
  17.  * SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY
  18.  * KIND, OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION,
  19.  * LOSS OF PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF
  20.  * THIRD PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.  HAS BEEN
  21.  * ADVISED OF THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON
  22.  * ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
  23.  * POSSESSION, USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  * 
  25.  * US Government Users Restricted Rights 
  26.  * Use, duplication, or disclosure by the Government is subject to
  27.  * restrictions set forth in FAR 52.227.19(c)(2) or subparagraph
  28.  * (c)(1)(ii) of the Rights in Technical Data and Computer Software
  29.  * clause at DFARS 252.227-7013 and/or in similar or successor
  30.  * clauses in the FAR or the DOD or NASA FAR Supplement.
  31.  * Unpublished-- rights reserved under the copyright laws of the
  32.  * United States.  Contractor/manufacturer is Silicon Graphics,
  33.  * Inc., 2011 N.  Shoreline Blvd., Mountain View, CA 94039-7311.
  34.  *
  35.  * OpenGL(R) is a registered trademark of Silicon Graphics, Inc.
  36.  */
  37.  
  38. #include <stdio.h>
  39. #include <GL/glut.h>
  40. #include <mui/mui.h>
  41. #include <dirent.h>
  42. #include <string.h>
  43. #include <sys/stat.h>
  44. #include <unistd.h>
  45. #include <stdlib.h>
  46. #include <mui/browser.h>
  47.  
  48. #define MAXFILES 400
  49. char    *filelist[MAXFILES];
  50. char    err[80];
  51. char    *dot = ".";
  52. char    *dotdot = "..";
  53. char    directory[300], originaldir[300];
  54. struct stat    d, dd;
  55. struct dirent    *dir;
  56.  
  57. DIR    *file;
  58. int    off;
  59.  
  60. extern int mui_singlebuffered;
  61. int selectedfile = -1;
  62. int cd(char *s);
  63. void pwd();
  64. void ls();
  65.  
  66. extern void    settlstrings(muiObject *obj, char **s);
  67. void    settltop(muiObject *obj, int top);
  68.  
  69. muiObject *tl, *vs, *l4;
  70.  
  71. void writeoutputfile(char *dir, char *file)
  72. {
  73.     FILE *f;
  74.     f = fopen(BROWSEFILE, "w");
  75.     fprintf(f, "D:%s\n", dir);
  76.     if (file)
  77.     fprintf(f, "F:%s\n", file);
  78.     fclose(f);
  79. }
  80.  
  81. void    controltltop(muiObject *obj, enum muiReturnValue value)
  82. {
  83.     float sliderval;
  84.  
  85.     if ((value != MUI_SLIDER_RETURN) && (value != MUI_SLIDER_THUMB)) return;
  86.     sliderval = muiGetVSVal(obj);
  87.     muiSetTLTop(tl, sliderval);
  88. }
  89.  
  90. void    handlefileselection(muiObject *obj, enum muiReturnValue value)
  91. {
  92.     char *fname;
  93.     int len;
  94.  
  95.     if (value == MUI_TEXTLIST_RETURN_CONFIRM) {
  96.     selectedfile = muiGetTLSelectedItem(obj);
  97.     fname = filelist[selectedfile];
  98.     len = strlen(fname);
  99.     if (fname[len-1] == '/') {
  100.         fname[len-1] = 0;
  101.         cd(fname);
  102.         return;
  103.     } else {
  104.         writeoutputfile(directory, fname);
  105.         exit(0);
  106.     }
  107.     }
  108.     if (value != MUI_TEXTLIST_RETURN) return;
  109.     selectedfile = muiGetTLSelectedItem(obj);
  110.     muiSetVSValue(vs, 1.0);
  111. }
  112.  
  113. void handleaccept(muiObject *obj, enum muiReturnValue value)
  114. {
  115.     char *fname;
  116.     int len;
  117.  
  118.     if (value != MUI_BUTTON_PRESS) return;
  119.     if (selectedfile == -1) return;
  120.     fname = filelist[selectedfile];
  121.     len = strlen(fname);
  122.     if (fname[len-1] == '/') {
  123.     fname[len-1] = 0;
  124.     cd(fname);
  125.     return;
  126.     } else {
  127.     writeoutputfile(directory, fname);
  128.     exit(0);
  129.     }
  130.     obj = 0;    /* for lint's sake */
  131. }
  132.  
  133. void handleoriginal(muiObject *obj, enum muiReturnValue value)
  134. {
  135.     if (value != MUI_BUTTON_PRESS) return;
  136.     cd(originaldir);
  137.     obj = 0;    /* for lint's sake */
  138. }
  139.  
  140. void handleupdir(muiObject *obj, enum muiReturnValue value)
  141. {
  142.     if (value != MUI_BUTTON_PRESS) return;
  143.     cd("..");
  144.     obj = 0;    /* for lint's sake */
  145. }
  146.  
  147. void handlecancel(muiObject *obj, enum muiReturnValue value)
  148. {
  149.     if (value != MUI_BUTTON_PRESS) return;
  150.     writeoutputfile(directory, 0);
  151.     exit(0);
  152.     obj = 0;    /* for lint's sake */
  153. }
  154.  
  155. void handletextbox(muiObject *obj, enum muiReturnValue value)
  156. {
  157.     char *s, *slash;
  158.  
  159.     if (value != MUI_TEXTBOX_RETURN) return;
  160.     s = muiGetTBString(obj);
  161.     if (0 == chdir(s)) {
  162.     pwd();
  163.     ls();
  164.     settlstrings(tl, filelist);
  165.     selectedfile = 0;
  166.     muiChangeLabel(l4, directory);
  167.     muiClearTBString(obj);
  168.     return;
  169.     }
  170.     /* hack up the path, if any */
  171.     slash = strrchr(s, '/');
  172.     if (slash == 0) {
  173.     slash = s-1;    /* to make filename == slash+1 */
  174.     } else {
  175.     if (*s == '/') { /* absolute path */
  176.         strncpy(directory, s, slash-s);
  177.         directory[slash-s] = 0;
  178.     } else {
  179.         strcat(directory, "/");
  180.         strncat(directory, s, slash-s);
  181.     }
  182.     }
  183.      /* now filename == slash+1 */
  184.     writeoutputfile(directory, slash+1);
  185.     exit(0);
  186. }
  187.  
  188. #define THUMBHEIGHT 20
  189. #define ARROWSPACE 40
  190.  
  191. void maketestui()
  192. {
  193.     muiObject *l1, *l2, *l3, *b1, *b2, *b3, *b4, *t;
  194.     int xmin, ymin, xmax, ymax;
  195.  
  196.     muiNewUIList(1);
  197.     l1 = muiNewBoldLabel(10, 475, "Directory:");
  198.     muiAddToUIList(1, l1, 0);
  199.     l4 = muiNewLabel(80, 475, "./");
  200.     muiAddToUIList(1, l4, 0);
  201.     l2 = muiNewBoldLabel(10, 430, "Set directory:");
  202.     muiAddToUIList(1, l2, 0);
  203.     b1 = muiNewButton(10, 100, 390, 415);
  204.     muiLoadButton(b1, "Up");
  205.     muiAddToUIList(1, b1, 1);
  206.     muiSetCallback(b1, handleupdir);
  207.     b2 = muiNewButton(10, 100, 355, 380);
  208.     muiLoadButton(b2, "Original");
  209.     muiAddToUIList(1, b2, 2);
  210.     muiSetCallback(b2, handleoriginal);
  211.     tl = muiNewTextList(120, 80, 370, 22);
  212.     muiAddToUIList(1, tl, 3);
  213.     muiGetObjectSize(tl, &xmin, &ymin, &xmax, &ymax);
  214.     vs = muiNewVSlider(xmax, ymin+2, ymax, 0, THUMBHEIGHT);
  215.     muiSetVSValue(vs, 1.0);
  216.     muiSetVSArrowDelta(vs, 10);
  217.     muiAddToUIList(1, vs, 4);
  218.     t = muiNewTextbox(120, 390, 40);
  219.     muiSetActive(t, 1);
  220.     muiAddToUIList(1, t, 5);
  221.     muiSetCallback(t, handletextbox);
  222.     l3 = muiNewBoldLabel(40, 50, "Open File:");
  223.     muiAddToUIList(1, l3, 0);
  224.     b3 = muiNewButton(130, 230, 9, 34);
  225.     muiLoadButton(b3, "Accept");
  226.     muiSetCallback(b3, handleaccept);
  227.     muiAddToUIList(1, b3, 5);
  228.     b4 = muiNewButton(250, 350, 9, 34);
  229.     muiLoadButton(b4, "Cancel");
  230.     muiSetCallback(b4, handlecancel);
  231.     muiAddToUIList(1, b4, 6);
  232.     muiSetCallback(vs, controltltop);
  233.     muiSetCallback(tl, handlefileselection);
  234.     
  235.     cd(directory);
  236.     strcpy(originaldir, directory);
  237. }
  238.  
  239. void main(int argc, char **argv)
  240. {
  241.     FILE *f;
  242.  
  243.     f = fopen(BROWSEFILE, "r");
  244.     parsebrowsefile(f);
  245.     fclose(f);
  246.     strcpy(directory, currentdirectoryname);
  247.     maketestui();
  248.     glutInit(&argc, argv);
  249.     if (argc > 1) mui_singlebuffered = 1;
  250.     glutInitWindowPosition(xcenter-200, ycenter-250);
  251.     glutInitWindowSize(400, 500);
  252.     if (mui_singlebuffered)
  253.     glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE );
  254.     else
  255.     glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  256.     glutCreateWindow("browser");
  257.     muiInit();
  258.     glutMainLoop();
  259. }
  260.  
  261. void errormsg(char *s)
  262. {
  263.     fprintf(stderr, "%s\n", s);
  264. }
  265.  
  266. void prname()
  267. {
  268.     directory[0] = '/';
  269.     if (off == 0)
  270.         off = 1;
  271.     directory[off] = 0;
  272. }
  273.  
  274. int dirlevels(char *s)
  275. {
  276.     int levels;
  277.  
  278.     for (levels = 0; *s; s++)
  279.     if (*s == '/')
  280.         levels++;
  281.     return(levels);
  282. }
  283.  
  284. int cat()
  285. {
  286.     register i, j;
  287.     char *name = directory + 1;    /* I love C */
  288.  
  289.     i = -1;
  290.     while (dir->d_name[++i] != 0) 
  291.     if ((off+i+2) > MAXNAMLEN - 1) {
  292.         prname();
  293.         return 1;
  294.     }
  295.     for(j=off+1; j>=0; --j)
  296.         name[j+i+1] = name[j];
  297.     off=i+off+1;
  298.     name[i] = '/';
  299.     for(--i; i>=0; --i)
  300.         name[i] = dir->d_name[i];
  301.     return 0;
  302. }
  303.  
  304. /* get the current working directory (the following 3 routines are from pwd.c) */
  305. void pwd()
  306. {
  307.     for(off = 0;;) {
  308.         if(stat(dot, &d) < 0) {
  309.             fprintf(stderr, "pwd: cannot stat .!\n");
  310.             exit(2);
  311.         }
  312.         if ((file = opendir(dotdot)) == NULL) {
  313.             fprintf(stderr,"pwd: cannot open ..\n");
  314.             exit(2);
  315.         }
  316.         if(fstat(file->dd_fd, &dd) < 0) {
  317.             fprintf(stderr, "pwd: cannot stat ..!\n");
  318.             exit(2);
  319.         }
  320.         if(chdir(dotdot) < 0) {
  321.             fprintf(stderr, "pwd: cannot chdir to ..\n");
  322.             exit(2);
  323.         }
  324.         if(d.st_dev == dd.st_dev) {
  325.             if(d.st_ino == dd.st_ino) {
  326.                 prname();
  327.                 chdir(directory);
  328.                 return;
  329.             }
  330.             do
  331.                 if ((dir = readdir(file)) == NULL) {
  332.                     fprintf(stderr, "pwd: read error in ..\n");
  333.                     exit(2);
  334.                 }
  335.             while (dir->d_ino != d.st_ino);
  336.         }
  337.         else do {
  338.                 if((dir = readdir(file)) == NULL) {
  339.                     fprintf(stderr, "pwd: read error in ..\n");
  340.                     exit(2);
  341.                 }
  342.                 stat(dir->d_name, &dd);
  343.         } while(dd.st_ino != d.st_ino || dd.st_dev != d.st_dev);
  344.         (void)closedir(file);
  345.         if (cat()) {
  346.             chdir(directory);
  347.             return;
  348.         }
  349.     }
  350. }
  351.  
  352. void freels()
  353. {
  354.     char **p;
  355.  
  356.     p = filelist;
  357.     while (*p != 0) {
  358.     free(*p);
  359.     *p = 0;
  360.     p++;
  361.     }
  362. }
  363.  
  364. int mystrcmp(char **s1, char **s2)
  365. {
  366.     return strcmp(*s1,*s2);
  367. }
  368.  
  369. void ls()
  370. {
  371.     DIR            *dirp;
  372.     int            i = 0;
  373.     int            len;
  374.     struct dirent    *dir;
  375.     struct stat        statbuf;
  376.     
  377.  
  378.     if ((dirp = opendir(directory)) == NULL) {
  379.     errormsg("bad directory\n");
  380.     return;
  381.     }
  382.     freels();
  383.     chdir(directory);
  384.     while ((dir = readdir(dirp)) != NULL) {
  385.     if (dir->d_name[0] == '.')
  386.         continue;
  387.     /*f = open(dir->d_name, O_RDONLY);
  388.     if (!f) 
  389.         continue;
  390.     if (!okfiletype(getfiletype(f)))
  391.         continue;
  392.     close(f);*/
  393.     stat(dir->d_name,&statbuf);
  394.     len = strlen(dir->d_name) + 1 + (statbuf.st_mode & S_IFDIR? 1 : 0);
  395.     filelist[i] = (char *)malloc(len);
  396.     strcpy(filelist[i], dir->d_name);
  397.     if (statbuf.st_mode & S_IFDIR) {
  398.         filelist[i][len-2] = '/'; filelist[i][len-1] = 0;
  399.     }
  400.     i++;
  401.     }
  402.     filelist[i] = 0;
  403.     qsort(&filelist[0], i, sizeof (char *), (int (*)(const void *, const void *))mystrcmp);
  404.     closedir(dirp);
  405. }
  406.  
  407. int cd(char *s)
  408. {
  409.     if(chdir(s) < 0) {
  410.     fprintf(stderr,"cannot open %s\n",s);
  411.     return -1;
  412.     }
  413.     pwd();
  414.     ls();
  415.     settlstrings(tl, filelist);
  416.     muiChangeLabel(l4, directory);
  417.     selectedfile = 0;
  418.     return 0;
  419. }
  420.